<?php
namespace App\Repositories\Admin;

use App\Models\Admin\JobCommentModel;
use App\Models\Admin\JobCompanyModel;
use App\Models\Admin\JobModel;
use App\Models\Admin\JobStaffModel;
use App\Repositories\BaseRepository;

class JobRepository extends BaseRepository
{
    protected $repository;
    protected $jobCom;
    protected $jobStaff;

    public function __construct()
    {
        $this->repository = new JobModel();
        $this->jobCom = new JobCompanyModel();
        $this->jobStaff = new JobStaffModel();
    }

    /**
     * 操作分页获取
     */
//    public function getIndexList()
//    {
//        $models = $this->repository->orderBy('id','asc')
//            ->paginate(10);
//        $models->limit = 10;
//        return $models;
//    }
    public function getIndexList($type)
    {
        $query = $this->repository->where('del',0)
            ->where('pid',0)
            ->where('cate',$type);
        $dataArr = array();
        if (!$query->count()) { return $dataArr; }
        foreach ($query->get() as $key => $model) {
            $dataArr[$key]['id'] = $model->id;
            $dataArr[$key]['name'] = $model->name;
            $subs = $this->repository->where('del',0)
                ->where('pid',$model->id)
                ->where('cate',$type)
                ->get();
            $subArr = array();
            if (count($subs)) {
                foreach ($subs as $key2 => $sub) {
                    $subArr[$key2]['id'] = $sub->id;
                    $subArr[$key2]['name'] = $sub->name;
                }
            }
            $dataArr[$key]['subs'] = $subArr;
        }
        return $dataArr;
    }

    /**
     * 获取指定职位列表
     */
    public function getJobsByCate($cate=0)
    {
        $query = $this->repository->where('del',0)
            ->where('pid',0);
        if ($cate) {
            $query = $query->where('cate',$cate);
        }
        return $query->get();
    }

    /**
     * 获取子职位列表
     */
    public function getJobsByPid($cate,$pid)
    {
        return $this->repository->where('del',0)
            ->where('pid',$pid)
            ->where('cate',$cate)
            ->get();
    }

    /**
     * 通过员工类型，获取职位列表
     */
    public function getListByStaffType($staffType)
    {
//        $query = $this->repository->where('del',0)
//            ->where('pid',0)
//            ->where('cate',$staffType);
//        $dataArr = array();
//        if (!$query->count()) { return $dataArr; }
//        foreach ($query->get() as $key => $model) {
//            $dataArr[$key]['id'] = $model->id;
//            $dataArr[$key]['name'] = $model->name;
//            $subs = $this->repository->where('del',0)
//                ->where('pid',$model->id)
//                ->where('cate',$staffType);
//            $subArr = array();
//            if (count($subs)) {
//                foreach ($subs as $key2 => $sub) {
//                    $subArr[$key2]['id'] = $sub->id;
//                    $subArr[$key2]['name'] = $sub->name;
//                }
//            }
//            $dataArr[$key]['subs'] = $subArr;
//        }
//        return $dataArr;
        $query = $this->repository->where('del',0)
            ->where('pid','>',0)
            ->where('cate',$staffType);
        $dataArr = array();
        if (!$query->count()) { return $dataArr; }
        foreach ($query->get() as $key => $model) {
            $parent = $this->repository->where('del',0)
                ->where('cate',$staffType)
                ->where('id',$model->pid)
                ->first();
            if ($parent) {
                $dataArr[$key]['id'] = $model->id;
                $dataArr[$key]['name'] = $parent->name.'--'.$model->name;
            }
        }
        return $dataArr;
    }

    /**
     * 父级
     */
    public function getParentList()
    {
        return $this->repository->where('pid',0)
            ->orderBy('id','asc')->get();
    }

    public function setAdd($data)
    {
        $data['created_at'] = time();
        return $this->repository->insertGetId($data);
    }

    public function setModify($data,$id)
    {
        $data['updated_at'] = time();
        return $this->repository->where('id',$id)
            ->update($data);
    }

    public function getShowOne($id)
    {
        return $this->repository->where('id',$id)->first();
    }

    /**
     * 清空招聘数据表
     */
    public function setClean()
    {
        $resJob = JobModel::truncate();
        if (!$resJob) { return -1; }
        $resCom = JobCompanyModel::truncate();
        if (!$resCom) { return -2; }
        $resJobStaff = JobStaffModel::truncate();
        if (!$resJobStaff) { return -3; }
        $resComment = JobCommentModel::truncate();
        if (!$resComment) { return -4; }
        return 0;
    }

    /**
     * WEB企业中心岗位列表
     * @param $uid 登陆用户UID
     * @param $page 页码
     */
    public function getWebJobIndex($uid,$page=1)
    {
        $pageStart = ($page - 1) * $this->limit;
        $models = $this->jobCom->where('uid',$uid)
            ->skip($pageStart)
            ->take($this->limit)
            ->orderBy('id','desc')
            ->get();
        $dataArr = array();
        if (!count($models)) { return $dataArr; }
        foreach ($models as $model) {
            $staffTotal = $this->jobStaff->where('del',0)
                ->where('cid',$uid)
                ->where('origin_id',$model->job)
//                ->where('status'1)
                ->count();
            $dataArr[] = [
                //岗位ID
                'jobId' => $model->id,
                //岗位名称
                'jobName' => $model->getJobName(),
                //参与人数
                'staffTotal' => $staffTotal,
                //当前状态
                'statusName' => '',
                //最后时间
                'updateTime' => '',
            ];
        }
    }
}